﻿/// <reference path="../../../Scripts/Jquery/jquery-1.7.1-vsdoc.js" />
/// <reference path="../../../Scripts/Jquery/jquery-ui-1.8.18.custom.min.js" />
/// <reference path="../../../Scripts/Jquery/jquery.validate-vsdoc.js" />

if (typeof (Solution) == 'undefined') Solution = {};
if (typeof (Solution.Admin) == 'undefined') Solution.Admin = {};
Solution.Admin.SysUser = {
    Init: function () {
    },
    gridID: '#List',
    CreateGrid: function (Url, UrlEdit) {
        var thisObj = this;
        var Pager = thisObj.gridID + 'Pager';
        $(thisObj.gridID).jqGrid({
            url: Url,
            datatype: 'json',
            mtype: 'GET',
            colNames: ['Id', 'Login Name', 'Full Name', 'Email', 'Birthday', 'Last Login', 'flag'],
            colModel: [
                          { name: 'Id', index: 'Id', width: 50, align: 'left', editable: false, editoptions: { readonly: true} },
                          { name: 'LoginName', index: 'LoginName', width: 150, align: 'left', editable: true },
                          { name: 'FullName', index: 'FullName', width: 200, align: 'left', editable: true, hidden: false, viewable: true, recreateForm: true },
                          { name: 'Email', index: 'Email', width: 200, align: 'left', editable: true },
                          { name: 'Birthday', index: 'Birthday', width: 100, align: 'left', editable: true, formatter: Solution.Core.FMDatetime },
                          { name: 'LastLogin', index: 'LastLogin', width: 100, align: 'left', editable: true, formatter: Solution.Core.FMDatetime },
                          { name: 'flag', index: 'flag', width: 50, align: 'left', editable: true, hidden: false, viewable: true, recreateForm: true }
                          ],
            pager: jQuery(Pager),
            rowNum: 20,
            rowList: [10, 20, 50],
            height: 400,
            width: 933,
            sortname: 'Id',
            sortorder: "desc",
            viewrecords: true,
            editurl: UrlEdit,
            multiselect: true,
            shrinkToFit: true,
            rownumbers: true,
            jsonReader: {
                root: "rows",
                page: "page",
                total: "total",
                records: "records",
                repeatitems: true,
                id: "0"
            },
            ondblClickRow: function (rowID) {
                var _URL = $('#edit').attr('attrUrl');
                Solution.Admin.SysUser.DialogEdit(rowID, _URL);
            },
            delSettings: function () {

            },
            onRightClickRow: function (rowid, iRow, iCol, e) {
                $("tr.jqgrow", this).contextMenu('myMenu1', {
                    bindings: {
                        'edit': function (trigger) {
                            // trigger is the DOM element ("tr.jqgrow") which are triggered                         
                            // $(gridID).editGridRow(trigger.id);
                            var _URL = $('#edit').attr('attrUrl');
                            Solution.Admin.SysUser.DialogEdit(trigger.id, _URL);
                        },
                        'add': function (/*trigger*/) {
                            //   $(gridID).editGridRow("new", addSettings);
                            Solution.Admin.SysUser.DialogCreate();
                        },
                        'del': function (trigger) {
                            if ($('#del').hasClass('ui-state-disabled') === false) {
                                // disabled item can do be choosed
                                $(thisObj.gridID).delGridRow(trigger.id, Solution.Admin.SysUser.delSettings);

                            }
                        }
                    },
                    onContextMenu: function (event/*, menu*/) {
                        var rowId = $(event.target).closest("tr.jqgrow").attr("id");
                        // $(gridID).setSelection(rowId);
                        // disable menu for rows with even rowids
                        $('#del').attr("disabled", Number(rowId) % 2 === 0);
                        if (Number(rowId) % 2 === 0) {
                            $('#del').attr("disabled", "disabled").addClass('ui-state-disabled');
                        } else {
                            $('#del').removeAttr("disabled").removeClass('ui-state-disabled');
                        }
                        return true;
                    }
                });
            },
            caption: '',
            beforeSelectRow: function (id, e) {             
                return Solution.Admin.jCommon.beforeSelectRow(thisObj.gridID, id, e);
            }
        });
        jQuery(this.gridID).jqGrid('navGrid', Pager,
                { add: false, edit: false, del: true }, //options
                {closeAfterEdit: true, afterSubmit: function (response, postdata) { if (response.responseText != '"err"') { Solution.Admin.Core.ShowNotif('s', 0); thisObj.ReloadGrid(); } else Solution.Admin.Core.ShowNotif('e', -1); return true; } },
                { closeAfterAdd: true, afterSubmit: function (response, postdata) { if (response.responseText != '"err"') { Solution.Admin.Core.ShowNotif('s', 0); thisObj.ReloadGrid(); } else Solution.Admin.Core.ShowNotif('e', -1); return true; } }
        ).navButtonAdd(Pager, {
            caption: " ",
            buttonicon: "ui-icon ui-icon-plus",
            onClickButton: function () {
                thisObj.DialogCreate();
            },
            position: "first"
        });

    },


    DialogCreate: function () {
        var option = {
            width: 490,
            title: 'Add user',
            resize: function (event, ui) {
            },
            close: function (event, ui) {
                Solution.Core.Wait('wait.............');
                Solution.Core.RemoveWaitThenOverlay();

            }
        }
        Solution.Core.Overlay();   
        $("#field input:[type = 'text']").val('');
        $("#field input:[type='password']").val('');
        $("#btnSubmit").attr('event', 'add');
        $("#btnCancel").attr('event', 'add');
        $('#LoginName').focus();
        $("#field").dialog(option);
        $('#LoginName').removeAttr('readonly');
    },
    DialogEdit: function (rowID, _URL) {
        $.ajax({
            url: _URL,
            data: $.toJSON({ myID: rowID }),
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (result) {
                console.log(result);
                $('#Id').val(result.Id);
                $('#LoginName').val(result.LoginName);
                $('#Email').val(result.Email);
                $('#flag').val(result.flag);

                if (result.Sex == 'Nam') {
                    $('#Sex').val('Male');
                }
                else
                    $('#Sex').val('Female');

                $('#FullName').val(result.FullName);
                $('#LoginName').attr('readonly', 'readonly');
                $('#Email').focus();
            },
            error: function () {
                Solution.Admin.Core.ShowNotif('e', -1, 'Ajax error');
            }

        });
        var option = {
            width: 490,
            title: 'Edit',
            overlay: 'background-color: red; opacity: 0.5',
            resize: function (event, ui) {
            },
            close: function (event, ui) {
                Solution.Core.RemoveOverlay();
            }
        }
        Solution.Core.Overlay();
        $("#btnSubmit").attr('event', 'edit');
        $("#btnCancel").attr('event', 'edit');
        $("#field").dialog(option);
    },
    SubmitEditCreate: function (_URL) {
        var btnSubmit = $("#btnSubmit");
        var thisObj = this;
        var _LoginName = $('#LoginName').val();
        var _Id = $('#Id').val();
        var _Email = $('#Email').val();
        var _flag = $('#flag').val();
        var _SexTmp = $('#Sex option:selected').val();
        var _Sex = 'Nam';
        if (_SexTmp == 'Female')
            _Sex = 'Nữ';
        var _FullName = $('#FullName').val();
        var _Password = $('#Password').val();
        if ($(btnSubmit).attr('event') == 'add') {
            $.ajax({
                url: _URL,
                data: $.toJSON({ oper: 'add', Id: _Id, LoginName: _LoginName, Email: _Email, flag: _flag, Sex: _Sex, FullName: _FullName, Password: _Password }),
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (result) {
                    if (result != 'err') {
                        $("#field").dialog("close");
                        thisObj.ReloadGrid();
                        Solution.Admin.Core.ShowNotif('s',0);
                    }
                    else {
                        Solution.Admin.Core.ShowNotif('e');
                    }

                },
                error: function () {
                    Solution.Admin.Core.ShowNotif('e', -1, 'Ajax error');
                }

            });

        }
        else {
            //edit            
            $.ajax({
                url: _URL,
                data: $.toJSON({ oper: 'edit', Id: _Id, LoginName: _LoginName, Email: _Email, flag: _flag, Sex: _Sex, FullName: _FullName, Password: _Password }),
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (result) {
                    if (result != 'err') {
                        $("#field").dialog("close");
                        thisObj.ReloadGrid();
                        Solution.Admin.Core.ShowNotif('s',0);
                    }
                    else {
                        Solution.Admin.Core.ShowNotif('e');
                    }

                },
                error: function () {
                      Solution.Admin.Core.ShowNotif('e',-1,'Ajax error');
                }

            });
        }
    },
    Cancel: function () {
        $("#field").dialog("close");
    },
    ReloadGrid: function () {
        Solution.Admin.jCommon.ReloadGrid(this.gridID);
    },
    ShowBin: function (Url, showBin) {
        Solution.Admin.jCommon.ShowBin(this.gridID, Url, showBin);
    },
    DeleteRows: function (_URL, delForever) {
        Solution.Admin.jCommon.DeleteRows(this.gridID, _URL, delForever);
    }

}


